package com.example.practice;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment5 extends Fragment {

    private View view;
    private GridView list4;
    public Fragment5() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment5, container, false);
        initView();
        initData();
        return view;
    }

    private void initData() {
        List<String> url=new ArrayList<>();
        List<String> urlname=new ArrayList<>();
        for (int i=0;i<5;i++){
            url.add("/sdcard/006unsovgy1g7ek5ovaxij30vf0vfqa3.jpg");
            urlname.add("图片名称"+i);
        }
        list4.setAdapter(new myGridView(url,urlname));
    }
    private void initView() {
        list4=view.findViewById(R.id.list_5);
    }
    private class myGridView extends BaseAdapter {
        List<String> url=new ArrayList<>();
        List<String> urlname=new ArrayList<>();

        public myGridView(List<String> url,List<String> urlname) {
            this.url=url;
            this.urlname=urlname;
        }

        @Override
        public int getCount() {
            return url.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }
        private class ViewHolder{
            ImageView imageView;
            TextView textView;
        }
        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            final myGridView.ViewHolder holder;
            if(view==null){
                view=LayoutInflater.from(getContext()).inflate(R.layout.vv_layout,null);
                holder=new ViewHolder();
                holder.imageView = view.findViewById(R.id.vv1);
                holder.textView = view.findViewById(R.id.vvt1);
                view.setTag(holder);
            }else {
                holder= (ViewHolder) view.getTag();
            }
            final Bitmap bitmap=getBitmap(url.get(i));
            holder.imageView.setImageBitmap(bitmap);
            holder.textView.setText(urlname.get(i));
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getContext(), TpActivity.class);
                    intent.putExtra("bitmap",url.get(i));
                    getActivity().startActivity(intent);

                }
            });
            return view;
        }
    }

    private Bitmap getBitmap(String url) {
        try {
            FileInputStream fis = new FileInputStream(url);
            return BitmapFactory.decodeStream(fis);  ///把流转化为Bitmap图片
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

}
